﻿using UnityEngine;
using System.Collections;

public class RollGauge : MonoBehaviour {

	UISprite sprite;
	//float maxFill = 0.238f;
	float maxFill = 1.0f;
	float minFill = 0.5f;
	//public float fillDelta = 0.3f;
	public float fillDelta = 0.5f;

	void Awake()
	{
		sprite = GetComponent<UISprite>();
	}

	public void StartGauge()
	{
		sprite.fillAmount = minFill;
		StopCoroutine("CoUpdateGauge");
		StartCoroutine("CoUpdateGauge");
	}

	IEnumerator CoUpdateGauge()
	{
		while(true)
		{
			sprite.fillAmount += fillDelta * Time.deltaTime;
			if(sprite.fillAmount >= maxFill)
			{
				sprite.fillAmount = maxFill;
				fillDelta = -fillDelta;
				glog.debug("plus fill="+sprite.fillAmount+"  "+fillDelta);
			}
			if(sprite.fillAmount<= minFill)
			{
				sprite.fillAmount = minFill;
				fillDelta = -fillDelta;
				glog.debug("minus fill="+sprite.fillAmount+"  "+fillDelta);
			}
			yield return null;
		}
	}

	public float StopGauge(bool ratioCheck)
	{
		StopCoroutine("CoUpdateGauge");
		if(ratioCheck)
		{
			float ratio = (sprite.fillAmount-minFill)/(maxFill-minFill);
			return ratio;
		}
		return 0;
	}
}
